﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AccessControlModule
{
    /// <summary>
    ///     操作的基类
    /// </summary>
    internal abstract class BaseAction<T,V>
    {
        #region 私有变量

        /// <summary>
        ///     私有变量，用来暂时存KV
        /// </summary>
        private readonly Dictionary<V, T> _obj;

        #endregion

        /// <summary>
        ///     构造函数
        /// </summary>
        /// <param name="obj"></param>
        public BaseAction(Dictionary<V, T> obj)
        {
            this._obj = obj;
        }


        /// <summary>
        ///     添加
        /// </summary>
        /// <typeparam name="T">需要添加的类的实体</typeparam>
        /// <param name="t">实体</param>
        /// <returns></returns>
        public abstract bool Add(T t)
        {
            if( typeof(V)== System.ValueType)

            _obj.Add(_obj.Count
        }

        /// <summary>
        ///     删除
        /// </summary>
        /// <typeparam name="T">需要删除类的实体</typeparam>
        /// <param name="t">实体</param>
        /// <returns></returns>
        public abstract bool Del(V id);

        /// <summary>
        ///     搜索
        /// </summary>
        /// <typeparam name="V">查询的值类型</typeparam>
        /// <param name="obj">查询的实体</param>
        /// <param name="id">查询的键</param>
        /// <returns></returns>
        public abstract SearchResult Search<V>(V id)
        {
            //检测值是否存在，不存在返回默认值(null或0)
            if (_obj.ContainsKey(id))
            {
                return new SearchResult(true, _obj[id]);
            }
            else
            {
                return new SearchResult(false,default(T));
            }
        }

        /// <summary>
        ///     查询结果类
        /// </summary>
        internal class SearchResult
        {
            public SearchResult(bool success, T result)
            {
                this._success = success;
                this._result = result;
            }

            private readonly bool _success;
            /// <summary>
            ///     查询是否成功
            /// </summary>
            public bool Success
            {
                get{return _success;}
            }
            private readonly T _result;
            /// <summary>
            ///     查询结果
            /// </summary>
            public T Result
            {
                get{return _result;}
            }
        }
    }
}
